import ItemData from "../data/ItemData";
import ItemLogic from "../logic/ItemLogic";

export default class KnapsackModel {
    constructor()
    {

    }
    private static _instance:KnapsackModel;
    
    public static get instance() : KnapsackModel {
        if (!this._instance)
        {
            this._instance = new KnapsackModel();
        }
        return this._instance
    }
    private _itemDataList:Array<ItemData> = []
    //重新设置_itemDataList
    private initItemData(itemList:Array<object>) {
        this._itemDataList = []
        for (const key in itemList) {
            if (itemList.hasOwnProperty(key)) {
                const item = itemList[key];
                let id:number = item["id"]
                let num:number = item["num"]
                let itemData:ItemData = new ItemData(id)
                itemData.num = num
                this._itemDataList.push(itemData)
            }
        }
    }
    
    public get itemDataList() : Array<ItemData> {
        return this._itemDataList
    }
    
    /**
     * 进入KnapsackView
     */
    public actionEnterKnapsackView(callFun:Function) {
        let itemList:Array<object> = ItemLogic.instance.itemList;
        this.initItemData(itemList)
        if(callFun)
        {
            callFun();
        }
    }
    //道具操作
    public actionUseItem(itemId:number,num:number,callFun:Function) {
        let itemList:Array<object> = ItemLogic.instance.useItem(itemId,num)
        this.initItemData(itemList)
        if(callFun)
        {
            callFun();
        }
    }
    
    //道具丢弃
    public actionLostItem(itemId:number,num:number,callFun:Function)
    {
        let itemList:Array<object> = ItemLogic.instance.lostItem(itemId,num)
        this.initItemData(itemList)
        if(callFun)
        {
            callFun();
        }
    }

    
    public Depot() {
        
    }

}
